#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    auto interface_list = QNetworkInterface::allInterfaces();
    foreach (auto &item, interface_list) {
        if ((item.flags() & (QNetworkInterface::IsUp|QNetworkInterface::IsLoopBack))!=0)
        {
            foreach (auto &_ip, item.addressEntries()) {
                if (_ip.ip().protocol() == QAbstractSocket::IPv6Protocol)
                    continue;
                ui->obj_cbb_ip->addItem(
                    _ip.ip().toString()
                    +(item.type()==QNetworkInterface::Virtual ? "*" : ""));
            }
        }

    }

    QString str_any_ip = "0.0.0.0";
    //添加一个0.0.0.0
    ui->obj_cbb_ip->addItem(str_any_ip);
    //设置anyip显示
    ui->obj_cbb_ip->setCurrentText(str_any_ip);

    //信号和槽
    connect(ui->obj_btn_tcp_start,&QPushButton::clicked,this,&Widget::on_server_btn_clicked);



}


Widget::~Widget()
{
    delete ui;
}

void Widget::on_server_btn_clicked()
{

    static QTcpServer *tcp_server = NULL;
    static QTcpServer *http_server = NULL;

    if (sender() == ui->obj_btn_tcp_start)   {

        if (tcp_server)
        {
            tcp_server->close();
            delete tcp_server ;
            tcp_server = NULL;
            ui->obj_btn_tcp_start->setText("开启服务");
            ui->obj_le_tcp_port->setEnabled(true);
            return;
        }

        tcp_server = new QTcpServer(this);
        //连接信号
        connect(tcp_server,&QTcpServer::newConnection,this,[&](){

            auto click_socket = tcp_server->nextPendingConnection();
            auto str_output = QObject::tr("a new connection! %1@%2")
                                  .arg(click_socket->peerAddress().toString())
                                  .arg(click_socket->peerPort());
            ui->obj_le_output->appendPlainText(str_output);
            m_client_list.append(click_socket);
            connect(click_socket,&QTcpSocket::readyRead,this,[=](){

                auto data = click_socket->readAll();
                auto str_output = QObject::tr("%1 %2@%3")
                                      .arg(QString(data).trimmed(),click_socket->peerAddress().toString())
                                      .arg(click_socket->peerPort());
                ui->obj_le_output->appendPlainText(str_output);
                click_socket->write(data);

            });
            connect(click_socket,&QTcpSocket::disconnected,this,[=](){


                for(int i=0;i<m_client_list.count();i++){
                    if (m_client_list.at(i) == click_socket)                    {
                        QString str_output = QObject::tr("%1@%2下线")
                                                 .arg(m_client_list.at(i)->peerAddress().toString())
                                                 .arg(m_client_list.at(i)->peerPort());
                        ui->obj_le_output->appendPlainText(str_output);
                        click_socket->deleteLater();
                        m_client_list.removeAt(i);
                        break;
                    }
                }


            });

        });
        auto port = ui->obj_le_tcp_port->text().toInt();
        tcp_server->listen(QHostAddress(ui->obj_cbb_ip->currentText()),port);
        ui->obj_btn_tcp_start->setText("关闭服务");
        ui->obj_le_tcp_port->setEnabled(false);


    }
    else   if (sender() == ui->obj_btn_http_start)   {

        if (http_server)
        {
            http_server->close();
            delete http_server ;
            http_server = NULL;
            ui->obj_btn_http_start->setText("开启服务");
            ui->obj_le_http_port->setEnabled(true);
            return;
        }

        http_server = new QTcpServer(this);

    }

}
